package com.iwdnb.gkgz;

import java.io.FileWriter;
import java.io.IOException;

import com.github.jknack.handlebars.Handlebars;
import com.github.jknack.handlebars.Template;
import org.icepear.echarts.Bar;
import org.icepear.echarts.Option;
import org.icepear.echarts.charts.bar.BarSeries;
import org.icepear.echarts.components.coord.cartesian.CategoryAxis;
import org.icepear.echarts.components.legend.Legend;
import org.icepear.echarts.components.title.Title;
import org.icepear.echarts.render.Engine;

public class EchartTest {

    public static void main(String[] args) {
        Bar bar = new Bar().addXAxis(
                new CategoryAxis()
                    .setData(new String[] {"中国", "美国", "俄罗斯", "法国", "英国"})
                    .setBoundaryGap(false))
            .setTitle(new Title().setText("五大常任理事国GDP统计").setTextAlign("auto").setLeft("center"))
            .addYAxis("万亿美元")
            .addSeries(new BarSeries().setName("2023年").setData(new Number[] {19.37, 27.36, 1.99, 3.02, 3.34}))
            .addSeries(new BarSeries().setName("2022年").setData(new Number[] {17.96, 25.44, 2.24, 2.78, 3.09}))
            .setLegend(new Legend().setShow(true).setData(new String[] {"2022年", "2023年"}).setAlign("auto")
                .setLeft("certer"));

        //定义文件名
        String file_name = "bar" + System.currentTimeMillis();

        //关闭动画
        Option option = bar.getOption();
        option.setAnimation(false);
        Engine engine = new Engine();
        Handlebars handlebars = new Handlebars();
        String html = "";

        //读模板生成html
        try {
            //模板路径
            Template template = handlebars.compile("echartTemplate/index");
            html = template.apply(engine.renderJsonOption(bar));
        } catch (IOException e) {
            System.out.println("模板未找到");
            e.printStackTrace();
        }

        //导出html文件
        try {
            //导出文件路径
            FileWriter fileWriter = new FileWriter(System.getProperty("user.dir") + "/aaa/echartTemplate/result.html");
            fileWriter.write(html);
            fileWriter.close();
        } catch (IOException e) {
            System.out.println("导出html失败");
            e.printStackTrace();
        }

    }
}
